import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/**
 * 省市区选择组件
 * @author: ljq
 * @since: 2019-01-12 15:46:51
 */
import * as React from 'react';
import { Cascader } from 'antd';
import pcadata from '../pcadata';
var data = pcadata;
var FILL_CHAR = '00';
if (localStorage.pcadata) {
    data = JSON.parse(localStorage.pcadata);
}
if (data['86'] && Object.keys(data['86'])[0].length === 2) {
    FILL_CHAR = '';
}
console.log('FILL_CHAR=' + FILL_CHAR);

var PcaSelect = function (_React$Component) {
    _inherits(PcaSelect, _React$Component);

    function PcaSelect() {
        _classCallCheck(this, PcaSelect);

        var _this = _possibleConstructorReturn(this, (PcaSelect.__proto__ || Object.getPrototypeOf(PcaSelect)).apply(this, arguments));

        _this.getProvince = function () {
            var provinces = data['86'];
            var options = [];
            for (var provinceCode in provinces) {
                if (!provinces.hasOwnProperty(provinceCode)) {
                    continue;
                }
                options.push({
                    value: provinceCode,
                    label: provinces[provinceCode]
                });
            }
            return options;
        };
        _this.getCity = function () {
            var provinces = data['86'];
            var options = [];
            for (var provinceCode in provinces) {
                if (!provinces.hasOwnProperty(provinceCode)) {
                    continue;
                }
                var p = {
                    value: provinceCode,
                    label: provinces[provinceCode],
                    children: []
                };
                var citys = data[provinceCode];
                for (var cityCode in citys) {
                    if (!citys.hasOwnProperty(cityCode)) {
                        continue;
                    }
                    p.children.push({
                        value: cityCode,
                        label: citys[cityCode]
                    });
                }
                options.push(p);
            }
            return options;
        };
        _this.getArea = function () {
            var provinces = data['86'];
            var options = [];
            for (var provinceCode in provinces) {
                if (!provinces.hasOwnProperty(provinceCode)) {
                    continue;
                }
                var p = {
                    value: provinceCode,
                    label: provinces[provinceCode],
                    children: []
                };
                var citys = data[provinceCode];
                for (var cityCode in citys) {
                    if (!citys.hasOwnProperty(cityCode)) {
                        continue;
                    }
                    var c = {
                        value: cityCode,
                        label: citys[cityCode],
                        children: []
                    };
                    var areas = data[cityCode];
                    for (var areaCode in areas) {
                        if (!areas.hasOwnProperty(areaCode)) {
                            continue;
                        }
                        c.children.push({
                            value: areaCode,
                            label: areas[areaCode]
                        });
                    }
                    p.children.push(c);
                }
                options.push(p);
            }
            return options;
        };
        _this.getOptions = function () {
            var level = _this.props.level;

            if (level === '1') {
                return _this.getProvince();
            }
            if (level === '2') {
                return _this.getCity();
            }
            if (level === '3') {
                return _this.getArea();
            }
            return [];
        };
        _this.renderText = function () {
            var defaultValue = _this.props.defaultValue;

            var provinceCode = defaultValue[0];
            var cityCode = defaultValue[1];
            var areaCode = defaultValue[2];
            var p = '';
            var c = '';
            var a = '';
            if (provinceCode) {
                p = data['86'][provinceCode] || '';
            }
            if (cityCode) {
                var cityProvinceCode = cityCode.substr(0, 2) + FILL_CHAR + FILL_CHAR;
                var citys = data[cityProvinceCode];
                if (citys) {
                    c = citys[cityCode] || '';
                }
            }
            if (areaCode) {
                var areaCityCode = areaCode.substr(0, 4) + FILL_CHAR;
                var areas = data[areaCityCode];
                if (areas) {
                    a = areas[areaCode] || '';
                }
            }
            return p + c + a;
        };
        return _this;
    }

    _createClass(PcaSelect, [{
        key: 'render',
        value: function render() {
            var _props = this.props,
                type = _props.type,
                defaultValue = _props.defaultValue;

            if (type === 'text') {
                return React.createElement(
                    'span',
                    this.props,
                    this.renderText()
                );
            }
            return React.createElement(Cascader, _extends({ placeholder: '\u8BF7\u9009\u62E9\u5730\u533A' }, this.props, { options: this.getOptions() }));
        }
    }]);

    return PcaSelect;
}(React.Component);

export default PcaSelect;